package com.my.mytest.kt.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.my.mytest.R
import com.my.mytest.kt.adapter.KtRvAdapter
import com.my.mytest.kt.bean.KtImgBean
import com.my.mytest.util.LogUtil
import com.my.mytest.util.SpaceItemDecoration
import com.my.mytest.util.ToastUtil
import kotlinx.android.synthetic.main.activity_kt_rv.*

class KtRvActivity : AppCompatActivity() {
    var data: ArrayList<Int> = ArrayList()

    var imgData: ArrayList<KtImgBean> = ArrayList();

    var adapter: KtRvAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kt_rv)

        initData()
        initView()
    }

    private fun initData() {
        data.add(R.mipmap.p1)
        data.add(R.mipmap.p2)
        data.add(R.mipmap.p3)
        data.add(R.mipmap.p4)
        data.add(R.mipmap.p5)
        data.add(R.mipmap.p6)
        data.add(R.mipmap.p7)
        data.add(R.mipmap.p8)
        data.add(R.mipmap.p9)
        data.add(R.mipmap.p10)
        data.add(R.mipmap.p11)
        data.add(R.mipmap.p12)
        data.add(R.mipmap.p13)
        data.add(R.mipmap.p14)
        data.add(R.mipmap.p15)
        data.add(R.mipmap.p16)
        data.add(R.mipmap.p17)
        data.add(R.mipmap.p18)
        data.add(R.mipmap.p19)
        data.add(R.mipmap.p20)
        data.add(R.mipmap.p21)

        for (i in 0 until data.size) {
            var ktBeam: KtImgBean = KtImgBean()
            ktBeam.setImageResource(data.get(i));
            imgData.add(ktBeam)
        }
    }

    private fun initView() {
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        rv_kt.layoutManager = layoutManager

        val spItemDe: SpaceItemDecoration = SpaceItemDecoration(this, 64, 2)
        rv_kt.addItemDecoration(spItemDe)

        adapter = KtRvAdapter(this)
        rv_kt.adapter = adapter

        adapter!!.setImgeData(imgData)

        adapter!!.setCallback(object : KtRvAdapter.CallBack {
            override fun onClick(positon: Int) {
                ToastUtil.show(this@KtRvActivity, "当前点击:" + positon)
            }

        })

        btn_kt.setOnClickListener {
            ToastUtil.show(applicationContext, "我被点击了")
            btn_kt.visibility = View.GONE
        }

    }
}